package com.budwk.app.sqfw.services.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.budwk.app.sqfw.dto.SqfwCompleteflowDTO;
import com.budwk.app.sqfw.models.Sqfw_rally;
import com.budwk.app.sqfw.models.Sqfw_reportFile;
import com.budwk.app.sqfw.services.SqfwRallyService;
import com.budwk.app.sqfw.services.SqfwReportFileService;
import com.budwk.app.sqfw.vo.SqfwReportFileVO;
import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.sys.services.SysUserService;
import com.budwk.starter.common.exception.BaseException;
import com.budwk.starter.database.service.BaseServiceImpl;
import com.budwk.starter.security.utils.SecurityUtil;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import java.util.Date;
import java.util.List;

/**
 * @author ZY.Wang
 * @since 2023/7/4
 */
@IocBean(args = {"refer:dao"})
public class SqfwReportFileServiceImpl extends BaseServiceImpl<Sqfw_reportFile> implements SqfwReportFileService {
    public SqfwReportFileServiceImpl(Dao dao) {
        super(dao);
    }

    @Inject
    private SysUserService sysUserService;

    @Inject
    private SqfwRallyService sqfwRallyService;

    @Override
    public List<SqfwReportFileVO> listReportFile(Sqfw_reportFile bean, String pageOrderName, String pageOrderBy) {
        Cnd cnd = Cnd.NEW();
        if (bean != null && StringUtils.isNotBlank(bean.getSqfwId())) {
            cnd.and("sqfw_id", "=", bean.getSqfwId());
        }
        cnd.orderBy("create_date", "desc");

        List<SqfwReportFileVO> resultList = Lists.newArrayList();

        List<Sqfw_reportFile> reportFileList = query(cnd);
        for (Sqfw_reportFile reportFile : reportFileList) {

            SqfwReportFileVO fileVO = new SqfwReportFileVO();
            BeanUtil.copyProperties(reportFile, fileVO);

            String createUserId = reportFile.getCreateBy();
            Sys_user createUser = sysUserService.getUserById(createUserId);
            if (createUser != null) fileVO.setCreateByUserName(createUser.getUsername());

            resultList.add(fileVO);
        }
        return resultList;
    }

    @Override
    public void insertRallyBriefing(SqfwCompleteflowDTO sqfwReportFile) {
        if(StrUtil.isEmpty(sqfwReportFile.getSqfwId())){
            throw new BaseException("sqfwId为空");
        }
        if(StrUtil.isEmpty(sqfwReportFile.getRallyId())){
            throw new BaseException("rallyId为空");
        }

        Sqfw_reportFile insertObj=BeanUtil.copyProperties(sqfwReportFile,Sqfw_reportFile.class);
        insertObj.setServiceRecordType("活动总结");
        insertObj.setReportName(sqfwReportFile.getContentTitle()==null?"活动总结":sqfwReportFile.getHeaderTitle());
        if(ObjectUtil.isNotEmpty(sqfwReportFile)){
            sqfwReportFile.setY(true);
            insertObj.setDocDetail(JSONUtil.toJsonStr(sqfwReportFile));
        }
        if(StrUtil.isEmpty(sqfwReportFile.getId())){
            Sqfw_reportFile getObj=this.fetch(Cnd.where(SqfwCompleteflowDTO::getSqfwId,"=",sqfwReportFile.getSqfwId()));
            if(ObjectUtil.isNotEmpty(getObj)){
                throw new BaseException("简报重复");
            }
            insertObj.setSqfwId(sqfwReportFile.getSqfwId());
            insertObj.setCreateDate(new Date());
            insertObj.setCreateBy(SecurityUtil.getUserId());
            this.insert(insertObj);
        }else {
            this.updateIgnoreNull(insertObj);
        }
    }

    @Override
    public SqfwCompleteflowDTO getReportBySqfwId(String sqfwId) {
        Sqfw_reportFile getObj=this.fetch(Cnd.where(Sqfw_reportFile::getSqfwId,"=",sqfwId));
        SqfwCompleteflowDTO sqfwCompleteflowDTO=new SqfwCompleteflowDTO();
        if(ObjectUtil.isNotEmpty(getObj)){
            sqfwCompleteflowDTO.setId(getObj.getId());
            sqfwCompleteflowDTO=JSONUtil.toBean(getObj.getDocDetail(),SqfwCompleteflowDTO.class);
            sqfwCompleteflowDTO.setId(getObj.getId());
            sqfwCompleteflowDTO.setY(true);

        }else {
            sqfwCompleteflowDTO.setY(false);
        }
        return sqfwCompleteflowDTO;
    }

    @Override
    public SqfwCompleteflowDTO getReportByRallyId(String rallyId) {
        Sqfw_reportFile getObj=this.fetch(Cnd.where("doc_detail->>'rallyId'","=",rallyId)
                .and(Sqfw_reportFile::getServiceRecordType,"=","活动总结"));
        SqfwCompleteflowDTO sqfwCompleteflowDTO=new SqfwCompleteflowDTO();
        if(ObjectUtil.isNotEmpty(getObj)){
            sqfwCompleteflowDTO.setId(getObj.getId());
            sqfwCompleteflowDTO=JSONUtil.toBean(getObj.getDocDetail(),SqfwCompleteflowDTO.class);
            sqfwCompleteflowDTO.setId(getObj.getId());
            sqfwCompleteflowDTO.setY(true);

        }else {
            sqfwCompleteflowDTO.setY(false);
        }
        return sqfwCompleteflowDTO;
    }


}
